//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Ontology.Mapping.Mappers
{
    using System.Collections.Generic;
    using System.Workflow.Runtime.Tracking;
    using Microsoft.Research.eResearch.Ontology.Lookup;
    using Microsoft.Research.eResearch.Blackboard.Common;
    using System;
    using System.Globalization;

    /// <summary>
    /// Implementation for UserTrackingRecord mapping mediator.
    /// </summary>
    [OntologicalTypeMapper(typeof(UserTrackingRecord))]
    public class UserTrackingRecordMapper : IOntologicalTypeMapper
    {
        /// <summary>
        /// Creates list of all the concept value pairs for the given object using lookup table.
        /// </summary>
        /// <param name="table">Instance of the lookup table.</param>
        /// <param name="input">Instance of the input object.</param>
        /// <returns>List of concept value pairs.</returns>
        public List<List<ConceptValuePair>> MapConceptValuePairs(IOntologyLookupTable table, object input)
        {
            List<List<ConceptValuePair>> messages = new List<List<ConceptValuePair>>();

            UserTrackingRecord utr = input as UserTrackingRecord;
            if (utr != null)
            {
                List<ConceptValuePair> message = CreateMessage(table, utr);
                messages.Add(message);
            }

            return messages;
        }

        /// <summary>
        /// Performs basic lookup and mapping for Custom user tracking record.
        /// </summary>
        /// <param name="table">Instance of the lookup table.</param>
        /// <param name="atr">Instance of the activity tracking record.</param>
        /// <returns>List of concept value pairs.</returns>
        private static List<ConceptValuePair> CreateMessage(IOntologyLookupTable table, UserTrackingRecord utr)
        {
            List<ConceptValuePair> message = new List<ConceptValuePair>();

            // Properties from TrackingRecord
            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "EventDateTime")))
            {
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(UserTrackingRecord), "EventDateTime"),
                    utr.EventDateTime.ToUniversalTime().Ticks.ToString()));
            }

            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "EventOrder")))
            {
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(UserTrackingRecord), "EventOrder"),
                    utr.EventOrder.ToString(CultureInfo.CurrentCulture)));
            }

            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "ActivityType")))
            {
                message.Add(new ConceptValuePair(
                   table.MapPropertyToConcept(typeof(UserTrackingRecord), "ActivityType"),
                   utr.ActivityType.ToString()));
            }

            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "Annotations")))
            {
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(UserTrackingRecord), "Annotations"),
                    utr.Annotations.Count >= 1 ? utr.Annotations[0] : utr.QualifiedName));
            }

            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "ContextGuid")))
            {
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(UserTrackingRecord), "ContextGuid"),
                    utr.ContextGuid.ToString()));
            }


            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "ParentContextGuid")))
            {
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(UserTrackingRecord), "ParentContextGuid"),
                    utr.ParentContextGuid.ToString()));
            }

            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "QualifiedName")))
            {
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(UserTrackingRecord), "QualifiedName"),
                    utr.QualifiedName));
            }

            if (utr.UserDataKey.Equals("IterationValue", StringComparison.Ordinal))
            {
                if (BlackBoardSettings.Concepts.IsConceptPresent(
                           table.MapPropertyToConcept(typeof(UserTrackingRecord), "IterationValue")))
                {
                    message.Add(new ConceptValuePair(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "IterationValue"),
                        utr.UserData.ToString()));
                }
            }
            else if (utr.UserDataKey.Equals("DataProductID", StringComparison.Ordinal))
            {
                if (BlackBoardSettings.Concepts.IsConceptPresent(
                           table.MapPropertyToConcept(typeof(UserTrackingRecord), "DataProductID")))
                {
                    message.Add(new ConceptValuePair(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "DataProductID"),
                        utr.UserData.ToString()));
                }
            }


            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "UserDataKey")))
            {
                message.Add(new ConceptValuePair(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "UserDataKey"),
                        utr.UserDataKey));

            }

            //Check if UserData is present in the blackboard concepts specified in the BlackBoardPolicyFile.xml by retrieving conceptName from ontology table
            if (BlackBoardSettings.Concepts.IsConceptPresent(
                        table.MapPropertyToConcept(typeof(UserTrackingRecord), "UserData")))
            {
                message.Add(new ConceptValuePair(
                     table.MapPropertyToConcept(typeof(UserTrackingRecord), "UserData"),
                     utr.UserData.ToString()));
            }

            return message;
        }
    }
}
